﻿//Copyright (c) 2010, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace yaTDL.Core.MVVM
{
    public static class ViewManager
    {
        #region Fields

        private static Dictionary<Type, Action<ViewModelBase, Action<object>>> registeredViews = new Dictionary<Type, Action<ViewModelBase, Action<object>>>();

        #endregion

        #region Public Methods

        public static void RegisterView(Action<ViewModelBase, Action<object>> showView, Type viewModelType)
        {
            if (HasRegisteredView(viewModelType))
            {
                registeredViews[viewModelType] = showView;
            }
            else
            {
                registeredViews.Add(viewModelType, showView);
            } 
        }

        public static void UnRegisterView(Type viewModelType)
        {
            registeredViews.Remove(viewModelType);
        }

        public static void ShowView(ViewModelBase viewModel)
        {
            ShowView(viewModel, null);
        }

        public static void ShowView(ViewModelBase viewModel, Action<Object> callback)
        {
            if (HasRegisteredView(viewModel.GetType()))
            {
                registeredViews[viewModel.GetType()](viewModel, callback);
                viewModel.IsShown = true;
            }
        }

        public static bool HasRegisteredView(Type viewModelType)
        {
            return registeredViews.ContainsKey(viewModelType);
        }

        #endregion
    }
}
